package com.wjm.blog.common.exception;

import com.wjm.blog.common.enums.ResponseEnum;
import lombok.Builder;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;

import java.io.Serializable;

/**
 * @Description: 业务异常定义
 * @Author: Ailos
 * @Date: 2020/4/20 10:51
 * @Version: 1.0
 */
@Data
@Builder
public class BizException extends RuntimeException implements Serializable {

    @Value("${wms.infor.errorCode:999}")
    private String errorCode;

    private static final long serialVersionUID = 1L;

    /**
     * 错误码
     */
    protected String code;
    /**
     * 错误信息
     */
    protected String msg;

    /**
     * 错误消息可变参数
     */
    protected Object[] args;

    public BizException(ResponseEnum responseEnum) {
        this.code = responseEnum.getCode();
        this.msg = responseEnum.getMessage();
    }

    public BizException(ResponseEnum responseEnum, Object... args) {
        this.code = responseEnum.getCode();
        this.msg = responseEnum.getMessage();
        this.args = args;
    }

    public BizException(ResponseEnum responseEnum, Throwable cause) {
        super(responseEnum.getCode(), cause);
        this.code = responseEnum.getCode();
        this.msg = responseEnum.getMessage();
    }

    public BizException(String msg) {
        super(msg);
        this.msg = msg;
    }

    public BizException(String msg, Object... args) {
        super(msg);
        this.msg = msg;
        this.args = args;
    }

    public BizException(String code, String msg) {
        super(code);
        this.code = code;
        this.msg = msg;
    }

    public BizException(String code, String msg, Throwable cause) {
        super(code, cause);
        this.code = code;
        this.msg = msg;
    }

    public BizException(String code, String module, String target, String content) {
        super(code);
        this.code = code;
        this.msg = content;
        this.args = new String[]{module, target};
    }

    public BizException(String module, String target, String content) {
        new BizException(errorCode, module, target, content);
    }
}
